package com.xyd.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xyd.pojo.Street;
import com.xyd.pojo.User;
import com.xyd.services.DistrictService;
import com.xyd.services.StreetService;
import com.xyd.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private DistrictService districtService;
    @Autowired
    private StreetService streetService;

    @RequestMapping("/getAllUser")
    public String getAllUser(Model model){
        List<User> users = userService.getAll();
        model.addAttribute("users",users);
        return "userList";
    }

    @RequestMapping("/login")
    public String login(User user){
        User login = userService.login(user);
        if (login != null){
           return "streetList";
        }
        return "redirect:/index.html";
    }
    @RequestMapping("/checkName")
    public void checkName(String nickName, HttpServletResponse response) throws IOException {
        User user = userService.findByName(nickName);
        if (user != null){
            response.getWriter().print("true");
        }else {
            response.getWriter().print("false");
        }
    }
    @RequestMapping("/regUser")
    public String regUser(User user,String repassword) {
        if (user.getPassword().equals(repassword)){
            int i = userService.addUser(user);
            if (i > 0){
                return "redirect:/index.html";
            }
        }
        return "error";
    }
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> getList(int pageIndex){
        Map<String,Object> map = new HashMap<>();
        PageHelper.startPage(pageIndex, 5);
        List<Street> streetList = streetService.getAll();
        PageInfo<Street> pageInfo = new PageInfo<>(streetList,3);
        List<Street> list = pageInfo.getList();
        if (streetList.size() >0){
            map.put("streetList",list);
            map.put("pageInfo",pageInfo);
            return map;
        }
        return null;
    }
    @RequestMapping("/detail")
    public String detail(String id,Model model){
//        ModelAndView mv = new ModelAndView();
        int sid = Integer.parseInt(id);
        Street street = streetService.getOneByStreetId(sid);
        if (street != null){
            model.addAttribute("street",street);
            return "detail";
        }
        return null;
    }

}
